import { resolve } from "path";
import { UserConfig, loadEnv } from "vite";
import { createVitePlugins } from "./build/vite";
import { root, env } from "./build/env";

// 路径查找
function pathResolve(dir: string) {
    return resolve(root, ".", dir);
}

// https://vitejs.dev/config/
export default (): UserConfig => {
    return {
        root,
        base: "./",
        server: {
            open: env.VITE_OPEN === "true",
            proxy: {
                "/api": {
                    target: "http://localhost:8070",
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/api/, "")
                }
            }
        },
        build: {
            rollupOptions: {
                input: {
                    main: "./index.html",
                    worker: "src/worker/defaultWorker.ts"
                },
                output: {
                    entryFileNames: "[name].bundle.js"
                }
            }
        },
        resolve: {
            extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".scss", ".css"],
            alias: [
                {
                    find: /@\//,
                    replacement: `${pathResolve("src")}/`
                }
            ]
        },
        plugins: createVitePlugins()
    };
};
